﻿using System;
using System.Collections;
using NHibernate;
using NHibernate.Cfg;
using System.Collections.Generic;

namespace Business
{
    public class Provider
    {
        private ISessionFactory _sessionFactory;
        public Provider()
        {
            _sessionFactory = DataObjects.Service.SessionFactory.GetInstance().GetNHibernateFactory();
        }
        
        public ArrayList List()
        {
            ArrayList arrRet = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                arrRet = (ArrayList)session.CreateCriteria(typeof(DataObjects.Provider)).List();
                tx.Commit();
            }
            return arrRet;
        }

        public ArrayList GetListQuickSearch(string condition)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                string sql = "select * from Provider where Provider_Id + Provider_Name like N'%" + condition + "%' and Status = 'CUR'";
                IQuery query = session.CreateSQLQuery(sql).AddEntity(typeof(DataObjects.Provider));
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                return arrTemp;
            }
        }

        public DataObjects.Provider GetObjectByPK(string proId)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                IQuery query = session.CreateSQLQuery("select top 1 * from Provider where Provider_ID =? and Status = 'CUR'").AddEntity(typeof(DataObjects.Provider));
                query.SetString(0, proId);
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                if (arrTemp.Count > 0)
                {
                    return (DataObjects.Provider)arrTemp[0];
                }
                return null;
            }
        }

        public ArrayList SearchProvider(object[] oParam)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                string sql = "exec sp_SearchProvider ?, ?, ?, ?, ?";
                IQuery query = session.CreateSQLQuery(sql).AddEntity(typeof(DataObjects.Provider));
                query.SetString(0, oParam[0].ToString());                   //Provider_Id
                query.SetString(1, oParam[1].ToString());                   //Provider_Name
                query.SetString(2, oParam[2].ToString());                   //Person_Represent
                query.SetString(3, oParam[3].ToString());                   //Person_Contact
                query.SetString(4, oParam[4].ToString());                   //Status

                arrTemp = (ArrayList)query.List();
                tx.Commit();

                return arrTemp;
            }
        }

        //code insert - update - delete 
        public void Insert(DataObjects.Provider oPro)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Save(oPro);
                tx.Commit();			        
            }
        }
        public void Update(DataObjects.Provider oPro)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Update(oPro);
                tx.Commit();		        
            }
        }
        public void DeleteByPK(string proId)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                DataObjects.Provider oPro = (DataObjects.Provider)session.Load(typeof(DataObjects.Provider), proId);
                session.Delete(oPro);
                tx.Commit();
            }
        }
    }
}
